import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2024-08-17
 * Time: 18:50
 */
public class BinaryTree {
    static class TreeNode {
        private char val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        A.left = B;
        A.right = C;
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode H = new TreeNode('H');
        B.left = D;
        B.right = E;
        E.right = H;
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        C.left = F;
        C.right = G;

        return A;
    }

    // 前序遍历  根左右
    void preOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历  左根右
    void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }
    // 后序遍历  左右根
    void postOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }

    //返回值是一个线性表的前序遍历
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();

        if(root == null) {
            return list;
        }
        list.add(root.val);
        List<Character> left = preorderTraversal(root.left);
        list.addAll(left);
        List<Character> right = preorderTraversal(root.right);
        list.addAll(right);
        return list;
    }


    // 获取树中 结点的个数 == 左子树的结点个数 + 右子树的结点个数 + 根结点
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    // 子问题思路-求叶子结点个数
    // 整棵树的叶子结点个数 == 左子树的叶子结点个数 + 右子树的叶子结点个数
    int getLeafNodeCount(TreeNode root){
        //判断叶子结点的条件, 如果该结点是叶子结点,返回 1
        if(root == null) {
            return 0;
        }else if(root.left == null && root.right == null){
            return 1;
        }else {
            return getLeafNodeCount(root.left) +
                    getLeafNodeCount(root.right);
        }
    }

    // 获取第K层节点的个数 == 左子树的第 k-1 层结点个数 + 右子树的第 k-1 层结点个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null) {
            return 0;
        }else if(k == 1) {
            return 1;
        }else {
            return getKLevelNodeCount(root.left, k-1) +
                    getKLevelNodeCount(root.right, k-1);
        }

    }

    // 获取二叉树的高度 == 左子树的高度和右子树的高度取最大值 + 1
    int getHeight(TreeNode root){
        if(root == null) {
            return 0;
        }else {
            int leftHeight = getHeight(root.left);
            int rightHeight = getHeight(root.right);
            return leftHeight > rightHeight ? leftHeight + 1 :
                    rightHeight + 1;
        }

    }

    // 检测值为value的元素是否存在 1.空树  2. 根是不是 val 不是继续  3.左子树和右子树
    TreeNode find(TreeNode root, int val){
        if(root == null) {
            return null;
        } else if (root.val == val) {
            return root;
        }else {
            TreeNode left = find(root.left, val);
            TreeNode right = find(root.right, val);
            return left != null ? left : right;
        }


    }

    //层序遍历 , 当队列里没有元素的时候 该二叉树就是遍历完成了
    void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
            System.out.print(cur.val + " ");
        }
        System.out.println();
    }


    // 判断一棵树是不是完全二叉树,
    // 二叉树遍历到 null 的时候停止, 此时队列里只有 null 就是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            //因为这个方法会在队列中存储 null ,所以会出现 cur == null的情况
            //一旦遍历到 null 的时候就结束循环,检查队列中是否出现 非null 的元素
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }

        }

        //把队列里的元素一个一个出队, 如果有元素 != null 就不是完全二叉树
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return true;
    }
}
